/*
 * 文件名：StsArCollectServiceImpl.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wuzhiyang
 * 修改时间：2020年3月20日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.job.sts.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.job.fia.service.ArPlanDetailService;
import top.infopub.job.fia.service.IncomeBillService;
import top.infopub.job.hr.service.OrgInfoService;
import top.infopub.job.mkt.service.DiscloseProService;
import top.infopub.job.sts.service.ArCollectService;
import top.infopub.job.sts.service.StsArCollectService;
import top.infopub.job.util.ParserUtil;
import top.infopub.model.hr.OrgInfo;
import top.infopub.model.mkt.DisclosePro;
import top.infopub.model.sts.ArCollect;
import top.infopub.tdp.util.BizParaUtil;

import com.google.common.collect.Maps;

@Service
public class StsArCollectServiceImpl implements StsArCollectService {

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private DiscloseProService discloseProService;

    @Autowired
    private IncomeBillService incomeBillService;

    @Autowired
    private ArCollectService arCollectService;

    @Autowired
    private ArPlanDetailService arPlanDetailService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void stsArCollectJob() {
        //查询所有交底的项目
        Map<String, Object> discloseProWhere = Maps.newHashMap();
        discloseProWhere.put("processStatus", "9");
        List<DisclosePro> disclosePros = discloseProService.selectByMapWhere(discloseProWhere);
        for (DisclosePro disclosePro : disclosePros) {
            ArCollect arCollect = new ArCollect();
            //往表里存放项目信息
            arCollect.setProCode(disclosePro.getProCode());
//            arCollect.setProName(disclosePro.getProName());
            //往表里存放公司信息
            OrgInfo orgInfo = new OrgInfo();
            orgInfo.setProjectCode(disclosePro.getProCode());
            orgInfo.setState(1);
            List<OrgInfo> orgInfos = orgInfoService.selectByEntityWhere(orgInfo);
            if (orgInfos.size() > 0) {
                // 注意：不归集南充项目
                if (StringUtils.endsWithIgnoreCase(orgInfos.get(0).getId(),
                    BizParaUtil.get("land_org_id"))) {
                    continue;
                }
                arCollect.setOrgProName(orgInfos.get(0).getCompName());
                arCollect.setHrOrgProId(orgInfos.get(0).getId());
                OrgInfo parentOrgInfo = new OrgInfo();
                parentOrgInfo.setId(orgInfos.get(0).getParentId());
                parentOrgInfo.setState(1);
                List<OrgInfo> parentOInfos = orgInfoService.selectByEntityWhere(parentOrgInfo);
                if (parentOInfos.size() > 0) {
                    arCollect.setHrOrgAreaName(parentOInfos.get(0).getCompName());
                    arCollect.setHrOrgAreaId(parentOInfos.get(0).getId());
                }
            }
            //存放时间信息
            arCollect.setBelongsYear(ParserUtil.getYearOfPrevMonth());
            arCollect.setBelongsMonth(ParserUtil.getMonthOfPrevMonth());
            arCollect.setStsMonth(ParserUtil.prevMonth());//上个月
            //回购款(月度应收款)
            Map<String, Object> backCashWhere = Maps.newHashMap();
            backCashWhere.put("belongsYear", arCollect.getBelongsYear());
            backCashWhere.put("months", arCollect.getBelongsMonth());
            backCashWhere.put("proCode", arCollect.getProCode());
            BigDecimal buyMonthWill = arPlanDetailService.selectBackCashByWhere(backCashWhere);
            arCollect.setBuyMonthWill(buyMonthWill);
            //回购款(月度已收款)
            Map<String, Object> incomeWhere = Maps.newHashMap();
            incomeWhere.put("incomeDateStart", ParserUtil.prevMonth());
            incomeWhere.put("incomeDateEnd", ParserUtil.currentMonth());
            incomeWhere.put("hrOrgProId", arCollect.getHrOrgProId());
            BigDecimal buyMonthAct = incomeBillService.selectIncomeOfCashInNine(incomeWhere);
            arCollect.setBuyMonthAct(buyMonthAct);
            //回购款(月度完成率)
            if (BigDecimal.ZERO != arCollect.getBuyMonthWill()
                && arCollect.getBuyMonthWill() != null) {
                BigDecimal buyMonthRate = arCollect.getBuyMonthAct().divide(
                    arCollect.getBuyMonthWill(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                    BigDecimal.valueOf(100));
                arCollect.setBuyMonthRate(buyMonthRate);
            }
            else {
                arCollect.setBuyMonthRate(BigDecimal.ZERO);
            }
            //项目运维收入(月度应收款)
            Map<String, Object> omIncomeWhere = Maps.newHashMap();
            omIncomeWhere.put("belongsYear", arCollect.getBelongsYear());
            omIncomeWhere.put("months", arCollect.getBelongsMonth());
            omIncomeWhere.put("proCode", arCollect.getProCode());
            BigDecimal omMonthWill = arPlanDetailService.selectOmIncomeByWhere(omIncomeWhere);
            arCollect.setOmMonthWill(omMonthWill);
            //项目运维收入(月度已收款)
            Map<String, Object> incomeWhereTen = Maps.newHashMap();
            incomeWhereTen.put("incomeDateStart", ParserUtil.prevMonth());
            incomeWhereTen.put("incomeDateEnd", ParserUtil.currentMonth());
            incomeWhereTen.put("hrOrgProId", arCollect.getHrOrgProId());
            BigDecimal omMonthAct = incomeBillService.selectIncomeOfCashInTen(incomeWhereTen);
            arCollect.setOmMonthAct(omMonthAct);
            //项目运维收入(月度完成率)
            if (BigDecimal.ZERO != arCollect.getOmMonthWill()
                && arCollect.getOmMonthWill() != null) {
                BigDecimal omMonthRate = arCollect.getOmMonthAct().divide(
                    arCollect.getOmMonthWill(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                    BigDecimal.valueOf(100));
                arCollect.setOmMonthRate(omMonthRate);
            }
            else {
                arCollect.setOmMonthRate(BigDecimal.ZERO);
            }
            //判断主表中是否已经存在开累的数据
            Map<String, Object> where = Maps.newHashMap();
            where.put("proCode", disclosePro.getProCode());
            where.put("dataFlag", 1);
            where.put("orderCondition", "STS_MONTH desc");
            List<ArCollect> arCollects = arCollectService.selectByMapWhere(where);
            if (arCollects != null && arCollects.size() > 0) {
                ArCollect collect = arCollects.get(0);
                if (collect.getBuyAddupWill() != null) {
                    //回购款(开累应收款)
                    arCollect.setBuyAddupWill(buyMonthWill.add(collect.getBuyAddupWill()));
                }
                else {
                    //回购款(开累应收款)
                    arCollect.setBuyAddupWill(buyMonthWill);
                }
                if (collect.getBuyAddupAct() != null) {
                    //回购款(开累已收款)
                    arCollect.setBuyAddupAct(buyMonthAct.add(collect.getBuyAddupAct()));
                }
                else {
                    //回购款(开累已收款)
                    arCollect.setBuyAddupAct(buyMonthAct);
                }
                if (collect.getOmAddupWill() != null) {
                    //项目运维收入(开累应收款)
                    arCollect.setOmAddupWill(omMonthWill.add(collect.getOmAddupWill()));
                }
                else {
                    //项目运维收入(开累应收款)
                    arCollect.setOmAddupWill(omMonthWill);
                }
                if (collect.getOmMonthAct() != null) {
                    //项目运维收入(开累已收款)
                    arCollect.setOmAddupActl(omMonthAct.add(collect.getOmMonthAct()));
                }
                else {
                    //项目运维收入(开累已收款)
                    arCollect.setOmAddupActl(omMonthAct);
                }
            }
            else {
                //回购款(开累应收款)
                arCollect.setBuyAddupWill(buyMonthWill);
                //回购款(开累已收款)
                arCollect.setBuyAddupAct(buyMonthAct);
                //项目运维收入(开累应收款)
                arCollect.setOmAddupWill(omMonthWill);
                //项目运维收入(开累已收款)
                arCollect.setOmAddupActl(omMonthAct);
            }
            //回购款(开累完成率)
            if (BigDecimal.ZERO != arCollect.getBuyAddupWill()) {
                BigDecimal buyAddupRate = arCollect.getBuyAddupAct().divide(
                    arCollect.getBuyAddupWill(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                    BigDecimal.valueOf(100));
                arCollect.setBuyAddupRate(buyAddupRate);
            }
            else {
                arCollect.setBuyAddupRate(BigDecimal.ZERO);
            }
            //项目运维收入(开累完成率)
            if (BigDecimal.ZERO != arCollect.getOmAddupWill()) {
                BigDecimal omAddupRate = arCollect.getOmAddupActl().divide(
                    arCollect.getOmAddupWill(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                    BigDecimal.valueOf(100));
                arCollect.setOmAddupRate(omAddupRate);
            }
            else {
                arCollect.setOmAddupRate(BigDecimal.ZERO);
            }
            //数据状态(默认为有效)
            arCollect.setDataFlag("1");
            //添加人 添加时间 最后修改人 最后修改时间
            arCollect.setAddBy("system");
            arCollect.setAddTime(new Date());
            arCollect.setUptBy("system");
            arCollect.setUptTime(new Date());
            arCollectService.insert(arCollect);
        }
    }
}